package com.ruoyi.temu.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.temu.service.ITemuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.temu.domain.TemuSpu;
import com.ruoyi.temu.service.ITemuSpuService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * spuController
 * 
 * @author ruoyi
 * @date 2023-09-03
 */
@RestController
@RequestMapping("/temu/spu")
public class TemuSpuController extends BaseController
{
    @Autowired
    private ITemuSpuService temuSpuService;

    private ITemuService temuService;

    /**
     * 查询spu列表
     */
    @PreAuthorize("@ss.hasPermi('temu:spu:list')")
    @GetMapping("/list")
    public TableDataInfo list(TemuSpu temuSpu)
    {
        startPage();
        List<TemuSpu> list = temuSpuService.selectTemuSpuList(temuSpu);
        return getDataTable(list);
    }

    /**
     * 导出spu列表
     */
    @PreAuthorize("@ss.hasPermi('temu:spu:export')")
    @Log(title = "spu", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TemuSpu temuSpu)
    {
        List<TemuSpu> list = temuSpuService.selectTemuSpuList(temuSpu);
        ExcelUtil<TemuSpu> util = new ExcelUtil<TemuSpu>(TemuSpu.class);
        util.exportExcel(response, list, "spu数据");
    }

    /**
     * 获取spu详细信息
     */
    @PreAuthorize("@ss.hasPermi('temu:spu:query')")
    @GetMapping(value = "/{spuId}")
    public AjaxResult getInfo(@PathVariable("spuId") Long spuId)
    {
        return success(temuSpuService.selectTemuSpuBySpuId(spuId));
    }

    /**
     * 新增spu
     */
    @PreAuthorize("@ss.hasPermi('temu:spu:add')")
    @Log(title = "spu", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TemuSpu temuSpu)
    {
        return toAjax(temuSpuService.insertTemuSpu(temuSpu));
    }

    /**
     * 修改spu
     */
    @PreAuthorize("@ss.hasPermi('temu:spu:edit')")
    @Log(title = "spu", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TemuSpu temuSpu)
    {
        return toAjax(temuSpuService.updateTemuSpu(temuSpu));
    }

    /**
     * 删除spu
     */
    @PreAuthorize("@ss.hasPermi('temu:spu:remove')")
    @Log(title = "spu", businessType = BusinessType.DELETE)
	@DeleteMapping("/{spuIds}")
    public AjaxResult remove(@PathVariable Long[] spuIds)
    {
        return toAjax(temuSpuService.deleteTemuSpuBySpuIds(spuIds));
    }

    /**
     * 从temu同步spu信息
     * @return
     */
    @PreAuthorize("@ss.hasPermi('temu:spu:sync')")
    @GetMapping(value = "/syncFromTemu")
    public AjaxResult syncFromTemu(){
        //
        logger.info("成功进入syncFromTemu");
        return toAjax(1);
//        return toAjax(temuService.syncProductFromTemu());
    }
}
